/**
 * @author       Richard Davey <rich@photonstorm.com>
 * @copyright    2018 Photon Storm Ltd.
 * @license      {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
 */

/**
 * Read an integer value from an XML Node.
 *
 * @function getValue
 * @since 3.0.0
 * @private
 *
 * @param {Node} node - The XML Node.
 * @param {string} attribute - The attribute to read.
 *
 * @return {integer} The parsed value.
 */

/**
 * Parse an XML font to Bitmap Font data for the Bitmap Font cache.
 *
 * @function ParseXMLBitmapFont
 * @since 3.0.0
 * @private
 *
 * @param {XMLDocument} xml - The XML Document to parse the font from.
 * @param {integer} [xSpacing=0] - The x-axis spacing to add between each letter.
 * @param {integer} [ySpacing=0] - The y-axis spacing to add to the line height.
 * @param {Phaser.Textures.Frame} [frame] - The texture frame to take into account while parsing.
 *
 * @return {BitmapFontData} The parsed Bitmap Font data.
 */
var ParseJsonBitmapFont = function (json, xSpacing, ySpacing, frame)
{
    if (xSpacing === undefined) { xSpacing = 0; }
    if (ySpacing === undefined) { ySpacing = 0; }

    var data = {};
    var info = json.font.info;
    var common = json.font.common;

    data.font = info.face;
    data.size = +info.size;
    data.lineHeight = +common.lineHeight + ySpacing;
    data.chars = {};

    var letters = json.font.chars.char;

    var adjustForTrim = (frame !== undefined && frame.trimmed);

    if (adjustForTrim)
    {
        var top = frame.height;
        var left = frame.width;
    }

    for (var i = 0; i < letters.length; i++)
    {
        var node = letters[i];

        var charCode = +node.id;
        var gx = +node.x;
        var gy = +node.y;
        var gw = +node.width;
        var gh = +node.height;

        //  Handle frame trim issues

        if (adjustForTrim)
        {
            if (gx < left)
            {
                left = gx;
            }

            if (gy < top)
            {
                top = gy;
            }
        }

        data.chars[charCode] =
            {
                x: gx,
                y: gy,
                width: gw,
                height: gh,
                centerX: Math.floor(gw / 2),
                centerY: Math.floor(gh / 2),
                xOffset: +node.xoffset,
                yOffset: +node.yoffset,
                xAdvance: +node.xadvance + xSpacing,
                data: {},
                kerning: {}
            };
    }

    if (adjustForTrim && top !== 0 && left !== 0)
    {
        // console.log('top and left', top, left, frame.x, frame.y);

        //  Now we know the top and left coordinates of the glyphs in the original data
        //  so we can work out how much to adjust the glyphs by

        for (var code in data.chars)
        {
            var glyph = data.chars[code];

            glyph.x -= frame.x;
            glyph.y -= frame.y;
        }
    }

    var kernings = json.font.kerning;
    var kernLen = kernings ? kernings.length : 0;
    for (i = 0; i < kernLen; i++)
    {
        var kern = kernings[i];

        var first = +kern.first;
        var second = +kern.second;
        var amount = +kern.amount;

        data.chars[second].kerning[first] = amount;
    }

    return data;
};

module.exports = ParseJsonBitmapFont;
